package com.clz.hyperit.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Comment;
import com.clz.hyperit.entity.Report;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.BlogService;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.GetComment;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.ReportVo;
import com.clz.hyperit.vo.UserArticle;


@RestController
@RequestMapping(value = "/blog")
public class BlogController {
	
	@Autowired
	private BlogService blogService;
	
	private ResultMessage result = null;
	
	private final Logger logger = LoggerFactory.getLogger(BlogController.class);
	
	@RequestMapping(value = "/getBlogs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getBlogs(Integer rank,Integer field,PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetail> bloglist = null;
		logger.debug("获取博客列表");
		bloglist = blogService.getBlogs(rank,field,pageInfo);
		return null;
	}
	/**
	 * 收藏
	 */
	@RequestMapping(value = "/collect", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> collect(Integer userdetail_id,Article article,Integer type){
		result = new ResultMessage();
		result.setServiceResult(true);
		Integer flag = blogService.saveCollect(userdetail_id,article,type);
		if(flag<=0){
			result.setResultInfo("收藏失败");
		}else{
			result.setResultInfo("收藏成功");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	
	/**
	 * 取消收藏
	 */
	@RequestMapping(value = "/deleteCollect", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteCollect(Integer userdetail_id,Integer id,Integer type){
		result = new ResultMessage();
		result.setServiceResult(true);
		blogService.deleteCollect(userdetail_id,id,type);
		result.setResultInfo("取消收藏");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获取收藏列表
	 */
	@RequestMapping(value = "/getCollect", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getCollect(Integer userdetail_id,Integer type,PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List list = blogService.getCollection(userdetail_id,type,pageInfo);
		result.getResultParm().put("collect_list",list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	
	/**
	 * 关注
	 */
	@RequestMapping(value = "/follow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> follow(Integer userdetail_id,Integer target_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Integer flag = blogService.saveFollow(userdetail_id,target_id);
		if(flag<=0){
			result.setResultInfo("关注失败");
		}else{
			result.setResultInfo("关注成功");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	
	/**
	 * 取消关注
	 */
	@RequestMapping(value = "/deleteFollow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteFollow(Integer userdetail_id,Integer target_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		blogService.deleteFollow(userdetail_id,target_id);
		result.setResultInfo("取消关注");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获取关注列表
	 */
	@RequestMapping(value = "/getFollow", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getFollow(Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetail> list = blogService.getFollowUserDetail(userdetail_id);
		result.getResultParm().put("ud_list",list);
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 打赏
	 * @param userdetail_id
	 * @param target_id
	 * @return
	 */
	@RequestMapping(value = "/reward", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> reward(Integer userdetail_id,Integer target_id){
		result = new ResultMessage();
		result.setServiceResult(true);

		int flag = blogService.updateReward(userdetail_id,target_id);
		if(flag==0){
			result.setResultInfo("打赏成功");
		}
		else if(flag==1){
			result.setResultInfo("HB不足");
			result.setServiceResult(false);
		}
		else{
			result.setResultInfo("不能打赏自己文章");
			result.setServiceResult(false);
		}

		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 举报
	 * @param report
	 * @return
	 */
	@RequestMapping(value = "/report", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> report(Report report){
		result = new ResultMessage();
		result.setServiceResult(true);
		blogService.saveReport(report);
		result.setResultInfo("举报成功");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 发表评论
	 * @param comment
	 * @return
	 */
	@RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> comment(Comment comment){
		result = new ResultMessage();
		result.setServiceResult(true);
		Comment comment_1 = blogService.saveComment(comment);
		if(comment_1!=null){
			result.setResultInfo("发送成功");
			result.getResultParm().put("comment",comment);
		}
		else{
			result.setServiceResult(false);
			result.setResultInfo("发送失败");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获取评论
	 * @param target_id
	 * @param type
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/getComment", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getComment(PageInfo pageInfo,Integer target_id,Integer type){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<GetComment> getcomment = null;
		List<Comment> comment_list = blogService.getCommentList(pageInfo,target_id,type);
		if (comment_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		}
		getcomment = getUserArticle(comment_list);
		result.getResultParm().put("getcomment",getcomment);
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 删除评论
	 * @param target_id
	 * @param type
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/deleteComment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteComment(Integer userdetail_id,Integer comment_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		int flag = blogService.deleteComment(userdetail_id,comment_id);
		if(flag==0){
			result.setServiceResult(false);
			result.setResultInfo("不能删除别人的评论");
		}
		else{
			result.setResultInfo("删除成功");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获取举报列表
	 * @param comment_list
	 * @return
	 */
	@RequestMapping(value = "/getReport", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getReport(){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<ReportVo> reportvo_list = null;
		reportvo_list = blogService.getReport();
		result.getResultParm().put("reportinfo",reportvo_list);
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 判断是否关注关系
	 * true已经关注
	 * false还没关注
	 * @return
	 */
	@RequestMapping(value = "/isFollow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> isFollow(Integer userdetail_id,Integer target_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		boolean flag = blogService.isfollow(userdetail_id,target_id);
		if(flag){
			result.setResultInfo("已经关注");
		}
		else{
			result.setResultInfo("已经关注");
		}	
		result.getResultParm().put("follow_flag",flag);
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}

	
	//方法
	private List<GetComment> getUserArticle(List<Comment> comment_list) {
		List<GetComment> list = new ArrayList<GetComment>();
		GetComment getComment = null;
		UserDetail ud = null;
		for(Comment comment : comment_list){
			ud = blogService.getCommentUserInfo(comment.getUserDetail_id());
			getComment = new GetComment(ud,comment);
			list.add(getComment);
		}
		return list;
	}
}
